Skip to content

feat(instrument): Add --stub-wasi flag to replace WASI imports with stubs#104

Merged
lwshang merged 3 commits intodfinity:mainfrom
shogochiai:feat/stub-wasi
Jan 27, 2026
Merged

feat(instrument): Add --stub-wasi flag to replace WASI imports with stubs#104
lwshang merged 3 commits intodfinity:mainfrom
shogochiai:feat/stub-wasi

Conversation

@shogochiai
Copy link
Contributor

@shogochiai shogochiai commented Jan 16, 2026

Problem

When using ic-wasm instrument on a canister WASM compiled with Emscripten, the resulting instrumented WASM contains WASI imports that IC rejects at install time:

Error: Wasm module has an invalid import section.
Module imports function 'fd_close' from 'wasi_snapshot_preview1'
that is not exported by the runtime.

Use Case

For languages that compile through Emscripten/C (e.g., Idris2), WASI imports are introduced by the toolchain, not by user code. This flag provides a practical workaround until native WASM backends become available.

Solution

Add --stub-wasi flag to the instrument subcommand that replaces WASI imports with local stub functions:

WASI Function Stub Behavior
fd_close Returns 0 (success)
fd_write Writes 0 to nwritten, returns 0
fd_read Writes 0 to nread, returns 0
fd_seek Writes 0 to newoffset, returns 0
environ_sizes_get Writes 0 to both params, returns 0
environ_get Returns 0
proc_exit Traps (unreachable)
Others Returns 0

Usage

ic-wasm canister.wasm -o instrumented.wasm instrument --stub-wasi

⚠️ Important Note

This is a workaround for edge cases (Emscripten, wasi-sdk).

The recommended approach is to build without WASI imports:

  • Rust: Use wasm32-unknown-unknown target with ic-cdk
  • C/C++: Configure toolchain to avoid WASI dependencies

Stub functions return success (0), which may hide real failures if your code actually depends on WASI functionality.

Test plan

  • Added stub_wasi test that verifies WASI imports are removed
  • All existing tests pass
  • cargo clippy and cargo fmt pass

🤖 Generated with Claude Code

…tubs

Problem:
When using `ic-wasm instrument` on a canister WASM compiled with Emscripten,
the resulting instrumented WASM contains WASI imports (fd_close, fd_write,
fd_seek, etc.) that IC rejects at install time:

  Error: Wasm module has an invalid import section.
  Module imports function 'fd_close' from 'wasi_snapshot_preview1'
  that is not exported by the runtime.

Solution:
Add `--stub-wasi` flag that replaces WASI imports with local stub functions
returning 0 (success) or trapping for proc_exit. This allows Emscripten-
generated WASMs to be instrumented and deployed to IC.

Usage:
  ic-wasm canister.wasm -o out.wasm instrument --stub-wasi
@cla-idx-bot
Copy link

cla-idx-bot bot commented Jan 16, 2026

Dear @shogochiai,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA.

If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged.

— The DFINITY Foundation

@shogochiai shogochiai closed this Jan 16, 2026
@shogochiai shogochiai reopened this Jan 16, 2026
lwshang and others added 2 commits January 21, 2026 09:12
…ng vs WASI stubbing

- Restructure Instrument section intro to list both capabilities
- Add subsection headers: "Execution tracing" and "Stubbing WASI imports"
- Move WASI stubbing docs to its own subsection after execution tracing
- Update CHANGELOG with --stub-wasi flag addition

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@lwshang lwshang merged commit 9bcb02a into dfinity:main Jan 27, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants